import { createApp } from "vue";
import App from "./App.vue";

// 引入路由
import Router from "./routers/index";

// 引入Vant
import Vant from "vant";

// 引入Vant样式
import "vant/lib/index.css";

//引入cookie
import VueCookies from "vue3-cookies";

//引入工具函数
import Tools from "./tools/tools";

//引入请求服务
import Request from "./services/request";

const app = createApp(App)
  .use(Router)
  .use(Vant)
  .use(VueCookies)
  .use(Tools)
  .use(Request)
  .mount("#app");

// 全局前置路由守卫
Router.beforeEach(async (to, from, next) => {
  let IsLogin = to.meta.IsLogin === true ? true : false;
  if (IsLogin === true) {
    //需要登录
    let result = await app.$login();
    
    if (result.code === 1) {
      
      app.$cookies.set("business", result.data); 
      app.$business = app.$cookies.get("business", result.data); 
    } else {
      app.$NotifyWarn(result.msg);

      app.$cookies.remove("business");

      next("/business/base/login");

      return false;
    }
    next();
  } else {
    //
    next();
  }
});
